﻿/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://bqqapicsharp.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace BQQAPIClient.Core.Protocol
{
    using System.Linq;
    using System.Text;
    using System.Threading;
    using BQQAPIClient.Core.Business;
    using BQQAPIClient.Core.Utility;

    [NotifyPackage(25, 201)]
    internal class UserVisistNotifyPackage : IParseOtherData
    {
        private AbstractNotifyPackage abstractNotifyPackage;
        internal UserVisit userVisit;

        public UserVisistNotifyPackage(AbstractNotifyPackage package)
        {
            this.abstractNotifyPackage = package;
        }

        public void ParseOtherData()
        {
            userVisit = new UserVisit();
            var result = abstractNotifyPackage.otherData.Where(tlv => tlv.t == UserVisit.TUserQQ).ToList();
            if (result.Count > 0) userVisit.UserQQ = result.First().v.GetLittleEndianUInt32();
            result = abstractNotifyPackage.otherData.Where(tlv => tlv.t == UserVisit.TMessageEncodingEnum).ToList();
            if (result.Count > 0) userVisit.MessageEncodingEnum = (MessageEncodingEnum)result.First().v[0];
            result = abstractNotifyPackage.otherData.Where(tlv => tlv.t == UserVisit.TMessage).ToList();
            if (result.Count > 0)
            {
                switch (userVisit.MessageEncodingEnum)
                {
                    case MessageEncodingEnum.GBK:
                        {
                            userVisit.Message = Encoding.GetEncoding("GBK").GetString(result.First().v);  
                            break;
                        }
                    case MessageEncodingEnum.UTF8:
                        {
                            result.First().v.Reverse();
                            userVisit.Message = Encoding.GetEncoding("GBK").GetString(result.First().v);  //腾讯这边的BUG
                            break;
                        }
                    case MessageEncodingEnum.UTF16:
                        {
                            userVisit.Message = Encoding.GetEncoding("UTF16").GetString(result.First().v);
                            break;
                        }
                }
            }

            Logger.Debug("UserVisistNotifyPackage : " + userVisit.ToString());

            if (BQQAPI.AssignKefuFunc != null)
            {
                ThreadPool.QueueUserWorkItem(state => BQQAPI.UserVisistNotifyAction(userVisit));
            }
        }
    }
}
